<?php

namespace App\Http\Controllers;

use App\Http\Resources\ArticleResourceCollection;
use App\Models\Article;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{
    public function index(Request $request)
    {
        return view('article', [
            'categoryOptions' => Category::query()->orderBy('id')->get()
        ]);
    }

    public function list(Request $request)
    {
        $perPage = $request->input('limit', 10);
        $articles = Article::query()
            ->with('category')
            ->orderBy('id')
            ->paginate($perPage);
        return ArticleResourceCollection::make($articles);
    }

    public function store(Request $request)
    {
        // 必填项
        $validator = Validator::make($request->input(), [
            'category_id' => 'required',
            'title' => 'required',
        ], [
            'category_id.required' => '分类ID不能为空',
            'title.required' => '标题不能为空',
        ]);
        if ($validator->stopOnFirstFailure()->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }
        $title = $request->input('title');
        $categoryId = $request->input('category_id');
        $category = Category::query()->find($categoryId);
        if (!$category) {
            return $this->error('分类不存在，请重新选择');
        }
        $article = new Article();
        $article->category_id = $category->id;
        $article->title = $title ?? '';
        $article->content = $request->input('content') ?? '';
        $article->save();
        return $this->ok('保存成功');
    }

    public function update(Request $request)
    {
        // 必填项
        $validator = Validator::make($request->input(), [
            'category_id' => 'required',
            'title' => 'required',
            'id' => 'required',
        ], [
            'category_id.required' => '分类ID不能为空',
            'title.required' => '标题不能为空',
        ]);
        if ($validator->stopOnFirstFailure()->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }
        $title = $request->input('title');
        $id = $request->input('id');
        $categoryId = $request->input('category_id');
        $category = Category::query()->find($categoryId);
        if (!$category) {
            return $this->error('分类不存在，请重新选择');
        }
        $article = Article::query()->find($id);
        if (!$article) {
            return $this->error('文章不存在');
        }
        $article->category_id = $category->id;
        $article->title = $title ?? '';
        $article->content = $request->input('content') ?? '';
        $article->save();
        return $this->ok('保存成功');
    }

    public function destroy(Request $request)
    {
        $id = $request->input('id');
        if (!$id) {
            return $this->error('请选择要删除的文章');
        }
        $article = Article::query()->find($id);
        if (!$article) {
            return $this->error('请选择要删除的文章');
        }
        $article->delete();
        return $this->ok('删除成功');
    }
}
